unit FBuscaPapel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, XDBGrids, DB, ZAbstractRODataset, ZDataset, StrUtils,
  ExtCtrls, Buttons;

type
  TFormBusca_Papel = class(TForm)
    Label1: TLabel;
    pBuscar: TPanel;
    qPapeles: TZReadOnlyQuery;
    dsPapeles_Alm: TDataSource;
    eCodigo: TEdit;
    Grid: TXDBGrid;
    eColor: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    pBotones: TPanel;
    BtnAceptar: TBitBtn;
    BtnCancelar: TBitBtn;
    Label9: TLabel;
    eGramaje: TEdit;
    Label11: TLabel;
    eAncho: TEdit;
    Label12: TLabel;
    eAlto: TEdit;
    cbTipo: TComboBox;
    procedure eCodigoChange(Sender: TObject);
    procedure BtnAceptarClick(Sender: TObject);
    procedure ePrecioKeyPress(Sender: TObject; var Key: Char);
    procedure FormActivate(Sender: TObject);
    procedure GridTitleClick(Column: TXColumn);
    procedure GridPaintColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TXColumn; Highlight: Boolean;
      Selections: TSelections; var Color: TColor; Font: TFont;
      var Image: TPersistent);
    procedure FormDestroy(Sender: TObject);
    procedure GridDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure qPapelesAfterOpen(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
    Id_Papel: Integer;
  end;

var
  FormBusca_Papel: TFormBusca_Papel;

implementation

uses ModDatos;

{$R *.dfm}

procedure TFormBusca_Papel.FormCreate(Sender: TObject);
var
  StrSQL: String;
begin
  cbTipo.Items.Clear;
  StrSQL := 'SELECT * ' +
            'FROM PAPELES_TIPOS ';
  md.AbreQuery( md.Query, StrSQL);
  While not md.Query.Eof do
  begin
    Grid.Columns[ 1].PickList.Add( md.Query.FieldByName( 'ID_TIPO_PAPEL').AsString);
    Grid.Columns[ 1].PickText.Add( md.Query.FieldByName( 'ID_TIPO_PAPEL').AsString + ' ' + md.Query.FieldByName( 'DESCRIPCION'  ).AsString);
    cbTipo.Items.Add( md.Query.FieldByName( 'ID_TIPO_PAPEL').AsString + ' ' + md.Query.FieldByName( 'DESCRIPCION'  ).AsString);
    md.Query.Next;
  end;
  MD.Query.Close;
end;


procedure TFormBusca_Papel.eCodigoChange(Sender: TObject);
var
  Asoc  ,
  Color ,
  StrSQL: String;

  Procedure SumaSQL( const Valor, Campo: String);
  begin
    if Valor <> '' then
      if Campo = 'ID_TIPO_PAPEL' then StrSQL := StrSQL + 'AND ' + Campo + ' = ' + LeftStr( Valor, 2) + ' '
                                 else StrSQL := StrSQL + 'AND ' + Campo + ' LIKE ' + QuotedStr( '%' + Valor + '%') + ' ';
  end;

  Function Asociacion_Papel_Proveed: String;
  var
    Select : String;
  begin
    Select := 'SELECT * ' +
              'FROM TIPOS_PAPEL_PROVEEDOR ';
    MD.AbreQuery( md.Query, Select);
    Result := '';
    While not md.Query.Eof do
    begin
      if Result <> '' then Result := Result + ' OR ';
      Result := Result + 'MA.IDPROVEEDOR = ' + QuotedStr( md.Query.FieldByName( 'IDPROVEEDOR').AsString) + ' AND MA.IDTIPODEPAPEL = '+ QuotedStr( md.Query.FieldByName( 'IDTIPODEPAPEL').AsString)+ ' ';
      MD.Query.Next;
    end;
    if Result <> '' then Result := '(' + Result + ')';
  end;

begin
  StrSQL := '';
  SumaSQL( eCodigo.Text, 'ID_PAPEL');
  SumaSQL( cbTipo.Text, 'ID_TIPO_PAPEL');
  SumaSQL( eGramaje.Text, 'GRAMAJE');
  SumaSQL( eAncho.Text, 'ANCHO');
  SumaSQL( eAlto.Text, 'ALTO');
  //Reemplazamos el primer AND por un Where
  StrSQL := StringReplace( StrSQL, 'AND', 'WHERE', []);


  //por si se quieren sacar todos los precio
  //SELECT P.*,
  //  PRECIO * ( 1 + T.CARGO_TARIFA0 / 100) PRECIO_T0,
  //  PRECIO * ( 1 + T.CARGO_TARIFA1 / 100) PRECIO_T1,
  //  PRECIO * ( 1 + T.CARGO_TARIFA2 / 100) PRECIO_T2
  //FROM PAPELES P
  //  INNER JOIN PAPELES_TIPOS T ON P.ID_TIPO_PAPEL = T.ID_TIPO_PAPEL;


  StrSQL := 'SELECT * ' +
            'FROM PAPELES ' +
            StrSQL +
            'ORDER BY ID_TIPO_PAPEL, GRAMAJE, ANCHO, ALTO, COLOR ';
  md.AbreQuery( qPapeles, StrSQL);

  if Id_Papel > 0 then
    qPapeles.Locate( 'ID_PAPEL', Id_papel, []);
end;

procedure TFormBusca_Papel.BtnAceptarClick(Sender: TObject);
begin
  if qPapeles.IsEmpty then ModalResult := mrCancel
                      else id_Papel    := qPapeles.fieldByName( 'ID_PAPEL').AsInteger;
end;

procedure TFormBusca_Papel.ePrecioKeyPress(Sender: TObject; var Key: Char);
begin
  If Key = '.' then Key := ','
end;

procedure TFormBusca_Papel.FormActivate(Sender: TObject);
begin
  eCodigoChange( Sender);
end;

procedure TFormBusca_Papel.GridTitleClick(Column: TXColumn);
begin
  Grid.OrderFields := Column.FieldName
end;

procedure TFormBusca_Papel.GridPaintColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TXColumn;
  Highlight: Boolean; Selections: TSelections; var Color: TColor;
  Font: TFont; var Image: TPersistent);
begin
  if qPapeles.FieldByName( 'ID_PAPEL').AsInteger = Id_Papel then Font.Style := [ fsBold]
end;

procedure TFormBusca_Papel.FormDestroy(Sender: TObject);
begin
  qPapeles.Close;
end;

procedure TFormBusca_Papel.GridDblClick(Sender: TObject);
begin
  BtnAceptarClick( Sender);
  ModalResult := mrOk;
end;

procedure TFormBusca_Papel.qPapelesAfterOpen(DataSet: TDataSet);
begin
  qPapeles.FieldByName( 'ID_TIPO_PAPEL'   ).Alignment := taLeftJustify;
end;

end.
